package com.hyjoy.service.impl;

import com.hyjoy.constant.Constant;
import com.hyjoy.dao.UserDao;
import com.hyjoy.dao.impl.UserDaoImpl;
import com.hyjoy.entity.User;
import com.hyjoy.service.UserService;

/**
 * Created by hyjoy on 2018/4/5.
 */
public class UserServiceImpl implements UserService {
    @Override
    public void regist(User user) throws Exception {
        UserDao userDao = new UserDaoImpl();
        userDao.save(user);
        // TODO: 发送激活邮件
    }

    @Override
    public User active(String code) throws Exception {
        UserDao userDao = new UserDaoImpl();
        User user = userDao.getByCode(code);
        if (user == null) {
            return null;
        }
        // 存在User
        user.setState(Constant.USER_IS_ACTIVE);
        user.setCode(null);
        userDao.update(user);
        return user;
    }

    /**
     * 用户登录
     *
     * @param username :
     * @param password :
     * @return
     * @throws Exception
     */
    @Override
    public User login(String username, String password) throws Exception {
        UserDao userDao = new UserDaoImpl();
        return userDao.getByUsernameAndPwd(username, password);
    }
}
